package com.lpc.android.abc.base.base;


import com.lpc.android.abc.base.api.ApiCallback;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;


/**
 * Created by lpc on 2016/12/17.
 */

public class BasePresenter<M extends IModel> {

    /**
     * 持有所有的Subscriptions
     */
//    protected CompositeSubscription mCompositeSubscription;

//    protected LoginContract.View view;

//    protected M mModel;

    public BasePresenter() {

//        mCompositeSubscription = new CompositeSubscription();
    }

    protected <T> Subscriber<T> newMySubscriber(final ApiCallback<T> apiCallback) {

        return new Subscriber<T>() {

            @Override
            public void onSubscribe(Subscription s) {

                apiCallback.onStart();

                s.request(Long.MAX_VALUE);
            }

            @Override
            public void onError(Throwable e) {

                apiCallback.onError(e);
            }

            @Override
            public void onComplete() {
                apiCallback.onCompleted();
            }

            @Override
            public void onNext(T t) {

                apiCallback.onNext(t);
            }

        };
    }

    protected <T> Subscriber<T> newProgressSubscriber(final IView view, final ApiCallback<T> apiCallback) {

        return new Subscriber<T>() {

            @Override
            public void onSubscribe(Subscription s) {

                apiCallback.onStart();
                if (view != null) {
                    view.showLoadingDialog();
                }

                s.request(Long.MAX_VALUE);
            }

            @Override
            public void onError(Throwable e) {

                apiCallback.onError(e);
                if (view != null) {
                    view.hideLoadingDialog();
                }
            }

            @Override
            public void onComplete() {
                apiCallback.onCompleted();
                if (view != null) {
                    view.hideLoadingDialog();
                }
            }

            @Override
            public void onNext(T t) {

                apiCallback.onNext(t);
            }

        };
    }

//    protected <T> Consumer<T> newProgressConsumer(final IView view, final ApiCallback<T> apiCallback) {
//
//        return new Consumer<T>() {
//            @Override
//            public void accept(@NonNull T t) throws Exception {
//
//            }
//        }
//    }

    public void onDestory() {

        onUnsubscribe();

//        if (mModel != null) {
//            mModel.onDestory();
//            mModel = null;
//        }

    }

    /*protected void addSubscription(Subscription subscription) {

        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }

        mCompositeSubscription.add(subscription);
    }*/

    /*protected <T> void addSubscription(Observable<T> observable, Subscriber<T> subscriber) {

        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }

        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }*/

    protected void onUnsubscribe() {
        /*if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }*/
    }
}
